package com.jeecg.mineattribute.controller;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by bscj on 2017/8/24.
 */
public class SaveFile {


    /**
     * 将数据库查询结果保存成文件并上传文件
     *
     * @param data 查询结果
     * @param separator 分隔符
     * @param localdictory 本地上传目录
     * @param filename 文件名称
     * @param remotedictory 远程上传目录
     */
    public void saveandupload(StringBuffer sb,String filetype, String localdictory, String filename, String remotedictory)
    {
        
        try {
        	 File dir = new File(localdictory);
             dir.mkdirs();
            //文件命名规则
            //生成文件前先删掉本地文件
            File del = new File(localdictory+File.separator+filename+"."+filetype);
            del.delete();
            FileWriter writer = new FileWriter(localdictory+File.separator+filename+"."+filetype,true);
//            StringBuffer sb = new StringBuffer();
//            for(int i=0;i<data.getTotRecord();i++){
//                for(int s=0;s<data.getTotField();s++){
//                	if(s<data.getTotField()-1)
//                       sb.append(data.getDataValue(s,i)+separator);
//                	else
//                	   sb.append(data.getDataValue(s,i));
//                }
//                sb.append("\r\n");
//            }
            writer.write(sb.toString());
            writer.flush();
            writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //拿到ftp连接信息
//        String hostname = "192.168.4.54";
//        int port = 21;
//        String username = "yh";
//        String password = "123456";
//        ContinueFTP myFtp = new ContinueFTP();
//        myFtp.uploadFile(hostname,port,username,password,remotedictory,localdictory+ File.separator+filename,filename);
    }

    public String arrayString(String[] arr){
        StringBuffer sb = new StringBuffer();
        for(String str:arr){
            sb.append(str);
        }
        sb.append("\r\n");
        return sb.toString();
    }
}
